<?php defined('SYSPATH') or die('No direct script access.');

/**
 * Akismet Service Driver Library
 * http://akismet.com/development/api/
 *
 * $Id: Akismet.php 158 2008-10-27 16:05:20Z alex.aperez $
 *
 * @package		Service Module
 * @author		Alex Sancho
 * @copyright	(c) 2008 Alex Sancho
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *	  notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *	  notice, this list of conditions and the following disclaimer in the
 *	  documentation and/or other materials provided with the distribution.
 * 3. Neither the name of copyright holders nor the names of its
 *	  contributors may be used to endorse or promote products derived
 *	  from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.	IN NO EVENT SHALL COPYRIGHT HOLDERS OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

class Service_Akismet_Driver extends Service_Driver
{
	protected $api_url = ".rest.akismet.com/1.1";
	protected $api_key;
	protected $blog_url;
	protected $user_agent = 'Kohana PHP 5 FrameWork | Akismet/1.11';

	/**
	 *	__construct
	 * 
	 * @param array $config
	 * @return void
	 * @access public
	 * 
	 */
	public function __construct($config = array())
	{
		$config += Kohana::config('service.akismet');
		
		foreach($config as $key => $val) 
		{
			$method = 'set_'.$key;

			if (method_exists($this, $method))
				$this->$method($val);
		}
	}
	
	/**
	 * set_blogurl
	 * 
	 * @param string $url
	 * @return object
	 * @access public
	 * 
	 */
	public function set_blogurl($url)
	{
		if ( ! Valid::url($url)) 
			throw new Kohana_User_Exception('Akismet Driver', Kohana::lang('service.invalid_url'));

		$this->blog_url = $url;
		return $this;
	}

	/**
	 * set_apikey
	 * 
	 * @param string $key
	 * @return object
	 * @access public
	 * 
	 */
	public function set_apikey($key)
	{
		$this->api_key = $key;
		return $this;
	}

	/**
	 * set_useragent
	 * 
	 * @param string $agent
	 * @return object
	 * @access public
	 * 
	 */
	public function set_useragent($agent)
	{
		if ( ! is_string($agent) or ! preg_match(":^[^\n/]*/[^ ]* \| Akismet/[0-9\.]*$:i", $agent))
			throw new Kohana_User_Exception('Akismet Driver', Kohana::lang('service.invalid_akismet_ua'));

		$this->user_agent = $agent;
		return $this;
	}

	/**
	 * is_spam
	 * 
	 * @param array $params
	 * @return boolean
	 * @access public
	 * 
	 */
	public function is_spam($params)
	{
		return $this->api_call('/comment-check', $params);
	}

	/**
	 * submit_spam
	 * 
	 * @param array $params
	 * @return boolean
	 * @access public
	 * 
	 */
	public function submit_spam($params)
	{
		return $this->api_call('/submit-spam', $params);
	}

	/**
	 * submit_ham
	 * 
	 * @param array $params
	 * @return boolean
	 * @access public
	 * 
	 */
	public function submit_ham($params)
	{
		$this->api_call('/submit-ham', $params);
	}

	/**
	 * api_call
	 * 
	 * @param string $api_url
	 * @param boolean $post
	 * @return mixed
	 * @access protected
	 * 
	 */
	protected function api_call($api_url, $params = array())
	{
		if (empty($params['user_ip']) or empty($params['user_agent'])) 
			throw new Kohana_User_Exception('Akismet Driver', Kohana::lang('service.missing_akismet_fields'));

		if ( ! isset($params['blog'])) 
			$params['blog'] = $this->blog_url;
		
		$this->post($params);

		return parent::api_call($api_url);
	}
	
	/**
	 * api_url
	 * 
	 * @param string $path
	 * @param string $separator
	 * @return string
	 * @access protected
	 * 
	 */
	protected function api_url($path, $separator = '&')
	{
		return $this->api_key . $this->api_url . $path;
	}

} //End Akismet Driver